package com.badger.spring.boot.security.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseCookie.ResponseCookieBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = { "登录接口" })
public class LoginController {

    @ApiOperation(value = "账号密码登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", paramType = "query", defaultValue = "ceshi_1"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query", defaultValue = "123456") })
    @PostMapping("/auth/login")
    public void login(@RequestParam(name = "username", required = true) String username,
            @RequestParam(name = "password", required = true) String password) {
    }

    @ApiOperation(value = "短信验证码")
    @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query")
    @GetMapping("/code/sms")
    public Map<String, String> smsCode(@RequestParam(name = "phone", required = true) String phone,
            HttpServletRequest request, HttpServletResponse response) {
        // 生成4位随机数
        StringBuffer code = new StringBuffer();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            code.append(r.nextInt(9));
        }
        // TODO 模拟发送短信到手机号
        System.out.println("发送短信到：" + phone + " 验证码：" + code);
        // 生成身份信息
        String identity = UUID.randomUUID().toString().replace("-", "");

        // 写入存储,本地演示，是单机项目，实际存储到三方组件中
        HttpSession session = request.getSession();
        session.setAttribute(identity, code.toString());
        // 信息写入 cookie以及header中
        ResponseCookieBuilder cookieBuild = ResponseCookie.from("identity_sms", identity);
        cookieBuild.path("/");
        cookieBuild.maxAge(60 * 5);
        cookieBuild.sameSite("None");
        response.setHeader(HttpHeaders.SET_COOKIE, cookieBuild.build().toString());// 写入cookie
        response.setHeader("identity_sms", identity);// 写入header
        // 数据通过返回体返回
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("identity_sms", identity);
        dataMap.put("code", code.toString());
        return dataMap;
    }

    @ApiOperation(value = "手机号+验证码")
    @ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "identity_sms", value = "身份信息", dataType = "String", paramType = "query") })
    @PostMapping("/auth/login/sms")
    public void phone(@RequestParam(name = "phone", required = true) String phone,
            @RequestParam(name = "code", required = true) String code,
            @RequestParam(name = "identity_sms", required = false) String identity_sms) {
    }
}
